package com.janushuang.algorithm.sort;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhijing.huang
 *         Created by zhijing.huang on 2014/9/26.
 */
public class InsertSort implements Sort {
    private static final Logger LOG = LoggerFactory.getLogger(InsertSort.class);
    @Override
    public void sort(int[] data) {
        int loopCount = 0;
        int len=data.length;
        for (int i = 1; i < len; i++) {
            ++loopCount;
//            LOG.debug("i={}",i);
            for (int j = i; (j>0)&&(data[j]<data[j-1]); j--) {
                ++loopCount;
//                LOG.debug("j={}",j);
                SortUtils.swap(data,j,j-1);
//                LOG.debug("data={}",data);
            }
        }
//        LOG.info("loopCount={}",loopCount);
    }
}
